﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;

namespace PassportManage
{
    public partial class Form_TakeOut : Form
    {
        public Form_TakeOut()
        {
            InitializeComponent();
        }        
        private void Form_TakeOut_Load(object sender, EventArgs e)
        {
            toolStripComboBox1.SelectedIndex = 4;
            try
            {
                CVRSDK.CVR_CloseComm();
                int iPort;
                for (iPort = 1001; iPort <= 1016; iPort++)
                {
                    iRetUSB = CVRSDK.CVR_InitComm(iPort);
                    if (iRetUSB == 1)
                    {
                        break;
                    }
                }

                for (iPort = 1; iPort <= 4; iPort++)
                {
                    iRetCOM = CVRSDK.CVR_InitComm(iPort);
                    if (iRetCOM == 1)
                    {
                        break;
                    }
                }

                if ((iRetCOM == 1) || (iRetUSB == 1))
                {
                    toolStripButton2.Visible = true;
                }
                else
                {
                    toolStripButton2.Visible = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                toolStripButton2.Visible = false;
            }
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string Condition = toolStripTextBox1.Text;
            switch (toolStripComboBox1.SelectedIndex)
            {
                case 0://条码
                    passportInfoTableAdapter1.FillByLikeTaskCode(passportManageDataSet1.PassportInfo,Condition);
                    break;
                case 1://姓名
                    passportInfoTableAdapter1.FillByName(passportManageDataSet1.PassportInfo, Condition);
                    break;
                case 2://身份证
                    passportInfoTableAdapter1.FillByCardID(passportManageDataSet1.PassportInfo, Condition);
                    break;
                case 3://证件号
                    passportInfoTableAdapter1.FillByCardCode(passportManageDataSet1.PassportInfo, Condition);
                    break;
                case 4://业务编号
                    passportInfoTableAdapter1.FillByOprationNumber(passportManageDataSet1.PassportInfo, Condition);
                    break;
            }
            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                dr.Cells["layerDataGridViewTextBoxColumn"].Value = ((int)dr.Cells["layerDataGridViewTextBoxColumn"].Value) - 1;
                dr.Cells["cellDataGridViewTextBoxColumn"].Value = ((int)dr.Cells["cellDataGridViewTextBoxColumn"].Value) - 1;
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            if (dataGridView1.Rows[e.RowIndex].Selected)
            {                
                PassportInfoData p = Common.PassportInfoDataFactory.Construct(passportManageDataSet1.PassportInfo, e.RowIndex);
                p.Layer++;
                p.Cell++;
                BLL.GetPassportInfoFormServer check = new BLL.GetPassportInfoFormServer(p.TaskCode);
                if (!check.CheckIcmApply(p.OprationNumber))
                    return;
//                 BLL.PassportSerialPort PSP = new BLL.PassportSerialPort((byte)p.Pillar);
//                 PSP.Display(p.Layer, p.Cell, 3);
                BLL.MovingPlatform MP = new BLL.MovingPlatform();                
                if(!MP.Moving(p.Pillar,p.Cell,false))
                    return;
                BLL.GetPassportPosition GetP = new BLL.GetPassportPosition();
                if(GetP.GetPosition(false, p))
                {                    
                    if (GetP.PositionList.Count == 1)
                    {
                        int Pillar = ((BLL.Position)(GetP.PositionList[0])).Pillar;
                        int Layer = ((BLL.Position)(GetP.PositionList[0])).Layer;
                        int Cell = ((BLL.Position)(GetP.PositionList[0])).Cell;
                        if (p.Pillar == Pillar && p.Layer == Layer && p.Cell == Cell)
                        {
                            MessageBox.Show("取出成功");
                            BLL.PassportInfo bPI = new BLL.PassportInfo();
                            if (!bPI.TakeOutPassport(p))
                            {
                                MessageBox.Show("取出时，数据库操作出现异常!");
                            }
                            BLL.GetPassportInfoFormServer doCardIssue = new BLL.GetPassportInfoFormServer(p.TaskCode);
                            doCardIssue.doCardIssue(p.OprationNumber);                                
                        }
                        else
                        {                            
                            //PSP.Display(p.Layer, p.Cell, 5);
                            MessageBox.Show("取出位置不正确，请放回，并重新操作");
                        }                       
                    }
                    else if (GetP.PositionList.Count > 1)
                    {
                        bool bOk = false;
                        for (int i = 0; i < GetP.PositionList.Count; i++)
                        {
                            BLL.Position OutP = (BLL.Position)GetP.PositionList[i];
                            int Pillar = OutP.Pillar;
                            int Layer = OutP.Layer;
                            int Cell = OutP.Cell;
                            if (p.Pillar == Pillar && p.Layer == Layer && p.Cell == Cell)
                            {
                                bOk = true;
                                MessageBox.Show("取出成功");
                                BLL.PassportInfo bPI = new BLL.PassportInfo();
                                if (!bPI.TakeOutPassport(p))
                                {
                                    MessageBox.Show("取出时，数据库操作出现异常!");
                                }                               
                                break;
                            }
                        }
                        if (!bOk)
                        {
                            MessageBox.Show("当前护照并未被取出");
                        }
                        WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                        log.LogType = LogType.探测相关.ToString();
                        log.LogContent = string.Format("护照{0},{1}---", p.TaskCode, p.Name);
                        for (int i = 0; i < GetP.PositionList.Count; i++)
                        {
                            log.LogContent += string.Format(
                                                     "柱子:{0},层:{1},单元格:{2}   ",
                                                    ((BLL.Position)(GetP.PositionList[i])).Pillar,
                                                    ((BLL.Position)(GetP.PositionList[i])).Layer,
                                                    ((BLL.Position)(GetP.PositionList[i])).Cell
                                                );
                        }
                        log.LogRemark = "取出时出现多个位置";
                        log.InserSQLLog();
                    }
                    toolStripButton1_Click(null, null);
                }
                if (!MP.Moving(p.Pillar, p.Cell, true))
                {
                    WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                    log.LogType = LogType.探测相关.ToString();
                    log.LogContent = string.Format("电机未进行回转:柱子:{0},单元格:{1}", p.Pillar, p.Cell);                    
                    log.LogRemark = "电机转动";
                    log.InserSQLLog();
                }
            }
        }

        private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)//扫描结束
            {
                toolStripButton1_Click(null, null);
            }
        }
        int iRetUSB = 0, iRetCOM = 0;
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                if ((iRetCOM == 1) || (iRetUSB == 1))
                {

                    int authenticate = CVRSDK.CVR_Authenticate();
                    if (authenticate == 1)
                    {
                        int readContent = CVRSDK.CVR_Read_Content(4);
                        if (readContent == 1)
                        {                            
                            try
                            {
                                byte[] number = new byte[30];
                                int length = 36;
                                CVRSDK.GetPeopleIDCode(ref number[0], ref length);
                                toolStripTextBox1.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(number);
                                toolStripComboBox1.SelectedIndex = 2;
                                toolStripButton1_Click(null, null);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
                        }
                        else
                        {
                            MessageBox.Show("读卡操作失败！");
                            return;
                        }
                    }
                    else
                    {
                        MessageBox.Show("未放卡或卡片放置不正确");
                    }
                }
                else
                {
                    MessageBox.Show("初始化失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Form_TakeOut_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }
    }
}
